package com.example.administrator.szb.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.bean.MaCode;
import com.example.administrator.szb.bean.UserBase;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.Contants;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.JPushUtils;
import com.example.administrator.szb.util.QTLog;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.util.Toasts;

import java.util.HashMap;
import java.util.Map;


public class TelUpdateActivity extends MVPBaseActivity {
    private int type=4;//所需要修改类型1呢称、2邮箱、3个性签名、4手机号5用户头像
    private TextView bar_title,bar_right;
    private LinearLayout activity_my_info_edit;
    private EditText phoneEdit;
    private EditText maEdit;
    private TextView getMaText;
    private String code = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tel_update);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        bar_title = (TextView) findViewById(R.id.bar_title);
        bar_right = (TextView) findViewById(R.id.bar_right);
        activity_my_info_edit = (LinearLayout) findViewById(R.id.activity_my_info_edit);
        phoneEdit = (EditText) findViewById(R.id.phoneEdit);
        maEdit = (EditText) findViewById(R.id.maEdit);
        getMaText = (TextView) findViewById(R.id.getMaText);
        bar_right.setVisibility(View.VISIBLE);
        bar_right.setText("确定");
        bar_title.setText("修改手机号");
    }

    @Override
    protected void initEvent() {
        getMaText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = getStr();
                if (s==null){
                    return;
                }
                requestMa();
            }
        });
        bar_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getStr()==null||getStr2()==null){
                    return;
                }
                if (code.equals(getStr2())){
                    requestUpdate();
                }else {
                    DialogUtil.showToast(context,"验证码不正确");

                }
            }
        });
    }
    private String getStr(){
        String str = phoneEdit.getText().toString().trim();
        if (Contants.isEmpty(str)||!Contants.isMobileNO(str)){
            Toasts.show(context,"手机号格式不正确",1000);
            return null;
        }
        if (SPUtils.getDatasString(SPUtils.USER_PHONE).equals(str)){
            Toasts.show(context,"请输入不同的手机号",1000);
            return null;
        }

        return str;
    }
    private String getStr2(){
        String str = maEdit.getText().toString().trim();
        QTLog.e("验证码："+str);
        if (Contants.isEmpty(str)||!Contants.isNumeric(str)){
            Toasts.show(context,"验证码格式不正确",1000);
            return null;
        }

        return str;
    }


    private void requestUpdate(){
        String str = getStr();
        if (str==null){
            return;
        }
        Map<String,Object> params = new HashMap<>();
        params.put("code", HttpUtil.getParam());
        params.put("users_id", SPUtils.getUserId());
        params.put("content",str);
        params.put("type",type);
        HttpUtils.post(activity,URLAddress.URLBASE + URLAddress.EDIT_USER_INFO, params, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                SPUtils.setDatas(SPUtils.USER_PHONE,getStr());
                final UserBase userBase = JSON.parseObject(str,new TypeReference<UserBase>(){});
                JPushUtils.register(getStr(),userBase.getJpushpass());
                Toasts.show(context,str,1000);
                MyInfoActivity.isNeedRefresh=true;
                finish();
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }



    /**
     * 获取验证码
     */
    private void requestMa(){
        changeYzmStatu();
        //code
        String phone_num = getStr();
        Map<String,Object> map = new HashMap<>();

        map.put("tel", phone_num);
        map.put("type", "2");//1代表注册验证码
        map.put("code", HttpUtil.getParam());
        HttpUtils.post(activity,URLAddress.URLBASE + URLAddress.URL_YZM, map, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                MaCode maCode = JSON.parseObject(str,new TypeReference<MaCode>(){});
                code = maCode.getCode();
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });

    }


    /**
     * 验证码按钮状态改变60秒重新发送
     */
    private int time=60;
    private void changeYzmStatu() {
        getMaText.setEnabled(false);

        final Handler handlerTimeTask = new Handler();
        handlerTimeTask.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (time <= 0) {
                    getMaText.setEnabled(true);
                    getMaText.setText("获取验证码");
                    time = 60;
                } else {
                    getMaText.setText(time + "s");
                    handlerTimeTask.postDelayed(this, 1000);
                }
                time--;
            }
        }, 1000);

    }
    @Override
    protected void initData() {

    }
}
